<?php
namespace Company\Controller;
use Think\Controller;
class FeestatisticalController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	
	//月度统计
	public function otherfee_statistical_excel(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count_other_excel";
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		$page=$param['page'];
		$push_data['data']['p']=$page;
		
		$res=push($api_url,$push_data);
		$res=json_decode($res,true);

		$list=$res['data']['detail'];
		// foreach($list as $k=>$v){
				// $list[$k]['goods_no']=" ".$v['goods_no'];
				// $list[$k]['sn']=" ".$v['sn'];
				// $list[$k]['hs_code']=" ".$v['hs_code'];
				// $list[$k]['pn']=" ".$v['pn'];
				// $list[$k]['zccode']=" ".$v['zccode'];
				// $list[$k]['net_weight']=sprintf("%.2f",$v['net_weight']);
				// $list[$k]['gross_weight']=sprintf("%.2f",$v['gross_weight']);
		// }
		$xlsName  = "其它费用列表";
		$xlsCell  = array(
			array('count_time','统计时间'),
			array('money','金额'),
			array('remark','备注')
		);
		$Aexcel=A('Order');
		$Aexcel->exportExcel($xlsName,$xlsCell,$list);
		die;
		
	}
	//月度统计
	public function otherfee_statistical(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count_other";
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		
		$p=I('p')?I('p'):1;
		$push_data['data']['p']=$p;
		
		$res=push($api_url,$push_data);

		$res=json_decode($res,true);
		
		if($res['flag']){
			$listrows=20;
			$firstrow=($p-1)*$listrows;
			$count=$res['data']['count_data'][0]['total'];
			$list=$res['data']['detail'];
			
			$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			
			$this->assign("count",$count);
			$this->assign("list",$list);
			$this->assign('page',$show);// 赋值分页输出
		}else{
			$this->assign("list",0);
		}
		$this->display();
	}
	//对账单&余额接口
	public function fee(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/account";
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];

		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		if(!empty($param['operate_type'])){
			$push_data['data']['operate_type']=$param['operate_type'];//+：充值2-：扣费
		}
		$p=I('p')?I('p'):1;
		$push_data['data']['p']=$p;
		$push_data['data']['listrows']=20;
		
		$res=push($api_url,$push_data);
		// dump($push_data);
		
		$res=json_decode($res,true);
		// dump($res['data']);
		// die;
		if($res['flag']){
			$listrows=$push_data['data']['listrows'];
			// $firstrow=($p-1)*$listrows;
			$count=$res['data']['count'];
			$list=$res['data']['account'];
			$usable_money=$res['data']['usable_money'];
			$count_data_add=$res['data']['count_data'][0]['total_money'];
			$count_data_reduce=$res['data']['count_data'][1]['total_money'];
			
			$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			
			//统计金额-begin
			$push_data['data']['listrows']=$count;
			$push_data['data']['p']=1;
			$res_count=push($api_url,$push_data);
			$res_count=json_decode($res_count,true);
			// $count=$res['data']['count'];
			$list_count=$res_count['data']['account'];
			
			$count_money=0;
			foreach($list_count as $k=>$v){
				$count_money+=(!empty($v['money'])?$v['money']:0);
			}
			
			$this->assign("count_money",$count_money);
			//统计金额-end
			$this->assign("usable_money",$usable_money);
			$this->assign("count_data_add",$count_data_add);
			$this->assign("count_data_reduce",$count_data_reduce);
			
			$this->assign("count",$count);
			$this->assign("list",$list);
			$this->assign('page',$show);// 赋值分页输出
		}else{
			$this->assign("list",0);
		}
		$this->display();
	}
	//月度统计
	public function month_statistical_excel(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count_month_excel";
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		$page=$param['page'];
		$push_data['data']['p']=$page;
		
		$res=push($api_url,$push_data);
		$res=json_decode($res,true);
		
		$list=$res['data']['detail'];
		// foreach($list as $k=>$v){
				// $list[$k]['goods_no']=" ".$v['goods_no'];
				// $list[$k]['sn']=" ".$v['sn'];
				// $list[$k]['hs_code']=" ".$v['hs_code'];
				// $list[$k]['pn']=" ".$v['pn'];
				// $list[$k]['zccode']=" ".$v['zccode'];
				// $list[$k]['net_weight']=sprintf("%.2f",$v['net_weight']);
				// $list[$k]['gross_weight']=sprintf("%.2f",$v['gross_weight']);
		// }
		$xlsName  = "月度统计列表";
		$xlsCell  = array(
			array('count_time','统计时间'),
			array('order_num','订单总数'),
			array('tax','税费'),
			array('operate_fee','操作费'),
			array('freight_fee','运费'),
			array('pt_fee','平台使用费')
		);
		$Aexcel=A('Order');
		$Aexcel->exportExcel($xlsName,$xlsCell,$list);
		die;
		
	}
	//月度统计
	public function month_statistical(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count_month";
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		
		$p=I('p')?I('p'):1;
		$push_data['data']['p']=$p;
		
		$res=push($api_url,$push_data);
		$res=json_decode($res,true);
		
		if($res['flag']){
			$listrows=20;
			$firstrow=($p-1)*$listrows;
			$count=$res['data']['count_data'][0]['total'];
			$list=$res['data']['detail'];
			
			$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			
			$this->assign("count",$count);
			$this->assign("list",$list);
			$this->assign('page',$show);// 赋值分页输出
		}else{
			$this->assign("list",0);
		}
		$this->display();
	}
	//统计界面
	public function statistical(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count";
		$u=M('user');
		$param=I('param.','','trim');
		
		
		
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		if(!empty($param['start_time'])){
			$push_data['data']['start_time']=$param['start_time'];
		}
		if(!empty($param['end_time'])){
			$push_data['data']['end_time']=$param['end_time'];
		}
		
		$p=I('p')?I('p'):1;
		$push_data['data']['p']=$p;
		
		$res=push($api_url,$push_data);
		// dump($push_data);
		// dump($res);
		// die;
		$res=json_decode($res,true);
		
		if($res['flag']){
			$listrows=20;
			$firstrow=($p-1)*$listrows;
			$count=$res['data']['count_data'][0]['total'];
			$list=$res['data']['detail'];
			
					 
			$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			
			$this->assign("count",$count);
			$this->assign("list",$list);
			$this->assign('page',$show);// 赋值分页输出
		}else{
			$this->assign("list",0);
		}
		
		
		$this->display();
		
	}

	//费用明细
	public function statistical_detail(){
		$api_url="http://www.t-cang.com/oms/index.php/Api/ErpApi/count_detail";
		
		$u=M('user');
		$param=I('param.','','trim');
		if(empty($param['username'])){
			$username=session('company.username');
		}else{
			$username=$param['username'];
		}
		$u_info=$u->where(array('username'=>$username))->find();
		
		$count_time=$param['count_time'];
		$start_time=$count_time." 00:00:00";
		$end_time=$count_time." 23:59:59";
		
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];

		
		$push_data['data']['start_time']=$start_time;
		
		$push_data['data']['end_time']=$end_time;
		
		
		$p=I('p')?I('p'):1;
		$push_data['data']['p']=$p;
		
		$res=push($api_url,$push_data);
		// dump($push_data);
		
		$res=json_decode($res,true);
		// dump($res);
		// die;
		if($res['flag']){
			$listrows=20;
			$firstrow=($p-1)*$listrows;
			$count=$res['data']['count_data'];
			$list=$res['data']['detail'];
			
			$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			
			$this->assign("count",$count);
			$this->assign("list",$list);
			$this->assign('page',$show);// 赋值分页输出
		}else{
			$this->assign("list",0);
		}
		$this->display();
	}
	
}